package com.cskaoyan.service;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GoodsExample;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.bean.goodsbean.wxbean.TopicListBean;
import com.cskaoyan.bean.goodsbean.wxvo.TopicBean;
import com.cskaoyan.bean.goodsbean.wxvo.WxRelatedTopicVO;
import com.cskaoyan.bean.goodsbean.wxvo.WxTopicDetailVO;
import com.cskaoyan.bean.goodsbean.wxvo.WxTopicListVO;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: whyccz-mall-project
 * @description:
 * @create: 2021-06-08 19:59
 **/
@Service
public class WxTopicServiceImpl implements WxTopicService {
	@Autowired
	TopicMapper topicMapper;
	@Autowired
	GoodsMapper goodsMapper;

	@Override
	public WxTopicListVO getTopicList(Integer page, Integer size) {

		PageHelper.startPage(page,size);

		TopicExample topicExample = new TopicExample();
		topicExample.setOrderByClause("add_time"+" "+"desc");
		TopicExample.Criteria criteria = topicExample.createCriteria();
		criteria.andDeletedEqualTo(false);
		List<Topic> topics = topicMapper.selectByExample(topicExample);
		List<TopicListBean> topicList=new ArrayList<>();
		for (Topic topic : topics) {
			TopicListBean topicListBean = new TopicListBean();
			topicListBean.setId(topic.getId());
			topicListBean.setPicUrl(topic.getPicUrl());
			topicListBean.setReadCount(topic.getReadCount());
			topicListBean.setTitle(topic.getTitle());
			topicListBean.setSubtitle(topic.getSubtitle());
			topicListBean.setPrice(topic.getPrice().intValue());
			topicList.add(topicListBean);
		}
		WxTopicListVO topicListVO = new WxTopicListVO();
		topicListVO.setTopicList(topicList);
		return topicListVO;
	}

	@Override
	public WxTopicDetailVO getTopicDetail(Integer id) {

		TopicExample topicExample = new TopicExample();
		TopicExample.Criteria criteria = topicExample.createCriteria();
		List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);
		Topic topic = topics.get(0);

		GoodsExample goodsExample = new GoodsExample();
		GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
		criteria1.andIdEqualTo(1030005).andDeletedEqualTo(true);
		List<Goods> goods = goodsMapper.selectByExample(goodsExample);

		WxTopicDetailVO wxTopicDetailVO = new WxTopicDetailVO();


		wxTopicDetailVO.setTopic(topic);
		wxTopicDetailVO.setGoods(goods);
		return wxTopicDetailVO;
	}

	@Override
	public List<TopicBean> getRelatedTopic(Integer id) {

		TopicExample topicExample = new TopicExample();
		TopicExample.Criteria criteria = topicExample.createCriteria();
		criteria.andDeletedEqualTo(false).andIdNotEqualTo(id);
		List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);
		List<TopicBean> topicList=new ArrayList<>();
		int topicLimit=4;
		for (Topic topic : topics) {
			if (topicLimit>0){
				TopicBean topicBean = new TopicBean();
				topicBean.setId(topic.getId());
				topicBean.setContent(topic.getContent());
				topicBean.setPicUrl(topic.getPicUrl());
				topicBean.setPrice(topic.getPrice().intValue());
				topicBean.setDeleted(topic.getDeleted());
				topicBean.setReadCount(topic.getReadCount());
				topicBean.setSortOrder(topic.getSortOrder());
				topicBean.setTitle(topic.getTitle());
				topicBean.setSubtitle(topic.getSubtitle());
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				topicBean.setAddTime(simpleDateFormat.format(topic.getAddTime()));
				topicBean.setUpdateTime(simpleDateFormat.format(topic.getUpdateTime()));
				topicBean.setGoods(topic.getGoods());
				topicList.add(topicBean);
				topicLimit--;
			}
		}

		return topicList;
	}


}
